package com.example.lyl.yuejian.view.photo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.bean.NotifyEventBean;
import com.example.lyl.yuejian.bean.ResultBean;
import com.example.lyl.yuejian.net.NetTool;
import com.example.lyl.yuejian.util.ImageUtil;
import com.example.lyl.yuejian.util.SharePreferenceUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class AddPhotoActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.content_et)
    EditText contentEt;
    @BindView(R.id.choose_photo_btn)
    Button choosePhotoBtn;
    @BindView(R.id.send_btn)
    Button sendBtn;
    @BindView(R.id.photo_iv)
    ImageView photoIv;

    private final int ALBUM = 56;
    private Bitmap bitmap;
    private String picPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_photo);
        ButterKnife.bind(this);

        toolbar.setTitle("发布晒图");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            //是左上角按键出现
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    @OnClick({R.id.choose_photo_btn, R.id.send_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.choose_photo_btn:
                Intent albumIntent = new Intent(Intent.ACTION_PICK, null);
                albumIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(albumIntent, ALBUM);
                break;
            case R.id.send_btn:
                File file = new File(picPath);
                final RequestBody requestFile = RequestBody.create(MediaType.parse("image/png"), file);
                MultipartBody.Part body =
                        MultipartBody.Part.createFormData("img", file.getName(), requestFile);

                NetTool.getInstance().getApi()
                        .addPhoto(SharePreferenceUtils.getInt(this, "uid", 0), contentEt.getText().toString(), body)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<ResultBean>() {
                            @Override
                            public void accept(ResultBean resultBean) throws Exception {
                                Toast.makeText(AddPhotoActivity.this, "发布成功" +
                                        "", Toast.LENGTH_SHORT).show();
                                EventBus.getDefault().post(new NotifyEventBean("刷新晒图列表"));
                                finish();
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                Toast.makeText(AddPhotoActivity.this, "检查前网络", Toast.LENGTH_SHORT).show();
                            }
                        });
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ALBUM:
                if (resultCode == RESULT_OK) {
                    picPath = ImageUtil.getPath(AddPhotoActivity.this, data.getData());
                    bitmap = ImageUtil.fileToBitmap(picPath, 200);
                    photoIv.setImageBitmap(bitmap);
                }
                break;
        }
    }
}
